// 图片查看器
define(['backbone', './swiper'], function(Backbone, Swiper) {

	var PicCheckerView = Backbone.View.extend({
		initialize: function(opt) {
			opt = opt || {};
			opt.el && this.setElement(opt.el);
			this.renderEls();
			this.initSwiper();
			this.preventDefault();
		},
		events: {
			'click .checker-close': 'evt_hide'
		},
		renderEls: function() {
			this.els = {
				swiperContainer: this.$('.swiper-container'),
				pagination: this.$('.swiper-pagination')
			}
		},
		initSwiper: function() {
			this.picSwiper = new Swiper({
				el: this.els.swiperContainer,
				pagination: this.els.pagination,
				paginationType: 'fraction',
				onSlideChangeEnd: _.bind(this.saveIdx, this)
			});
		},
		preventDefault: function() {
			this.$el.on('touchmove', function(e) {
				e.preventDefault();
			});
		},
		show: function() {
			var _view = this;
			var _container = _view.$el;
			_container.animate({
				'translateY': '100%'
			}, 0, function() {
				_container.show().animate({
					'translateY': 0
				}, 300, 'ease-out', function() {
					_view.picSwiper &&
					_view.picSwiper.swiper.update();
					_view.picSwiper.swiper.slideTo(_view.activeIndex|0, 0)
				});
			});
		},
		saveIdx: function(swiper) {
			this.activeIndex = swiper.activeIndex|0;
		},
		evt_hide: function() {
			var _container = this.$el;
			_container.animate({
				'translateY': 0
			}, 0, function() {
				_container.animate({
					'translateY': '100%'
				}, 300, 'ease-out', function() {
					_container.hide();
				});
			});
		}
	});

	return PicCheckerView;

});